
import java.util.*;

// 必须定义 `ShowMeBug` 入口类和 `public static void main(String[] args)` 入口方法
public class ShowMeBug {
    static class Node {
        int id;
        int parentId;
        String name;

        public Node(int id, int parentId, String name) 				{
            this.id = id;
            this.parentId = parentId;
            this.name = name;
        }
    }

    public static void main(String[] args) {
        List<Node> nodeList = Arrays.asList(
                new Node(1, 0, "AA"),
                new Node(2, 1, "BB"),
                new Node(3, 1, "CC"),
                new Node(4, 3, "DD"),
                new Node(5, 3, "EE"),
                new Node(6, 2, "FF"),
                new Node(7, 2, "GG"),
                new Node(8, 4, "HH"),
                new Node(9, 5, "II"),
                new Node(10, 0, "JJ"),
                new Node(11, 10, "KK"),
                new Node(12, 10, "LL"),
                new Node(13, 12, "MM"),
                new Node(14, 13, "NN"),
                new Node(15, 14, "OO")
        );
        print(nodeList);
    }

    public static void print(List<Node> nodeList1) {
        HashMap<Integer,Node> map = new HashMap<>(nodeList1.size());
        for(int i=0;i<nodeList1.size();i++){
            Node node = nodeList1.get(i);
            map.put(node.id,node);
        }
        for(Map.Entry<Integer,Node> node: map.entrySet()){
            Node value = node.getValue();
            if(!map.containsKey(value.parentId)){
                System.out.println(value.name);
                print1(nodeList1,value.id,"  ");
            }
        }

    }
    public static  void print1(List<Node> nodeList1,int parentId,
                               String str){
        for(int i=0;i<nodeList1.size();i++){
            Node node = nodeList1.get(i);
            if(node.parentId == parentId){
                System.out.println(str+node.name);
                print1(nodeList1,node.id,str+"  ");
            }
        }
    }
}